package com.sdy.hospitalguard.web.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.Response;
import com.sdy.common.utils.StringUtil;
import com.sdy.hospitalguard.biz.dto.VisitManageDTO;
import com.sdy.hospitalguard.biz.service.ManagerService;
import com.sdy.hospitalguard.biz.service.VisitAppointmentService;
import com.sdy.hospitalguard.biz.utils.SearchBrowserKeywordsUtil;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: 王越洋
 * @version: v1.0
 * @description: com.sdy.hospitalguard.web.controller
 * @date:2020/5/26
 */
@RestController
@RequestMapping("/visit")
@Api(tags = "探视管理接口")
public class VisitAppointmentController extends BaseController {

    @Autowired
    private VisitAppointmentService visitAppointmentService;
    @Autowired
    private ManagerService managerService;

    @GetMapping("/visitPageQuery")
    @ApiOperation(value = "探视者分页查询")
    public Response visitPageQuery(String message, Integer areaId, String start, String end, Page page, HttpServletRequest request) throws Exception {
        Integer userId = getUserId(request);
        Integer roleId = managerService.getById(userId).getRoleId();
        if (roleId == 2 || roleId == 4) {
            return Response.error("当前用户不显示此界面!");
        }
        Integer userAreaId = managerService.getById(userId).getInpatientAreaId();
        if (StringUtil.isBlank(message)) {
            message = null;
        } else {
            message = URLDecoder.decode(message, "UTF-8");
        }
        if (StringUtil.isBlank(start) || StringUtil.isBlank(end)) {
            start = null;
            end = null;
        }
        if (StringUtil.isNotBlank(start)) {
            start = URLDecoder.decode(start, "UTF-8");
        }
        if (StringUtil.isNotBlank(end)) {
            end = URLDecoder.decode(end, "UTF-8");
        }
        IPage<VisitManageDTO> visitManageDTOIPage = visitAppointmentService.getVisitInformation(page, message, areaId, start, end, userAreaId);
        List<VisitManageDTO> visitManageDTOList = visitManageDTOIPage.getRecords();
        for (int i = 0; i < visitManageDTOList.size(); i++) {
            String phone = visitManageDTOList.get(i).getVisitPhone();
            visitManageDTOList.get(i).setVisitPhone(phone.substring(0, 3) + "****" +
                    phone.substring(7));
        }
        return Response.success(visitManageDTOIPage);
    }

    @GetMapping("/excelExport")
    @ApiOperation(value = "探视者Excel导出")
    public void excelExport(String message, Integer areaId, String start, String end,
                            HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer userId = getUserId(request);
        Integer roleId = managerService.getById(userId).getRoleId();
        if (roleId == 2 || roleId == 4) {
            new Throwable("当前用户不显示此界面!");
            return;
        }
        Integer userAreaId = managerService.getById(userId).getInpatientAreaId();
        if (StringUtil.isBlank(message)) {
            message = null;
        } else {
            message = URLDecoder.decode(message, "UTF-8");
        }
        if (StringUtil.isBlank(start) || StringUtil.isBlank(end)) {
            start = null;
            end = null;
        }
        if (StringUtil.isNotBlank(start)) {
            start = URLDecoder.decode(start, "UTF-8");
        }
        if (StringUtil.isNotBlank(end)) {
            end = URLDecoder.decode(end, "UTF-8");
        }
        List<VisitManageDTO> visitManageDTOList = visitAppointmentService.getVisitInformationList(message, areaId, start, end, userAreaId);
        /*for (int i=0; i<visitManageDTOList.size(); i++) {
            String phone = visitManageDTOList.get(i).getVisitPhone();
            visitManageDTOList.get(i).setVisitPhone(phone.substring(0, 3) + "****" +
                    phone.substring(7));
        }*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String datetime = sdf.format(new Date());
        String fileName = datetime + "探视记录.xlsx";
        boolean isMSIE = SearchBrowserKeywordsUtil.isMSBrowser(request);
        if (isMSIE) {
            /**
             * IE、360兼容版浏览器的乱码问题解决
             */
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } else {
            /**
             *万能乱码问题解决
             */
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        EasyExcel.write(response.getOutputStream(), VisitManageDTO.class).sheet("探视记录").doWrite(visitManageDTOList);
    }

}
